"use strict";
import {
	showMessage
} from './status'
import qs from 'qs'
let config = {
	baseURL: "/API", // 正式环境
	// baseURL: "http://192.168.0.59:8282", 
	// baseURL:'https://ycgov.zkzhimu.cn/api', // 正式环境
	timeout: 30 * 1000, // Timeout,
	heaader: {
		'Content-Type': 'application/x-www-form-urlencoded'
	}
};
const Message = (title, icon = 'none', duration = 1500) => {
	uni.showToast({
		title,
		duration,
		icon

	})
}

const toLogin = () => {
	uni.reLaunch({
		url: '/pagesC/login/index'
	});
}



// 这里区分是不是手机，手机不用代理，电脑调试用代理
if (uni.getSystemInfoSync().platform == "devtools") { //安卓真机
	config.baseURL = 'https://www.sytlzr.cn/api';
	// config.baseURL  = 'http://192.168.0.59:8282'

}

const requst = (params) => {
	config.heaader['Cookie'] = uni.getStorageSync('cookie');
	let url = config.baseURL + params.url;
	if (params.params) {
		const pa = qs.stringify(params.params);
		url += '?' + pa
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url, //仅为示例，并非真实接口地址。
			data: params.data,
			method: params.method,
			timeout: config.timeout,
			header: config.heaader,
			success: (res) => {
				// console.log(res)
				const {
					statusCode,
					data
				} = res;
				const message = showMessage(statusCode);



				if (statusCode == 200) {

					// if(data.code === 403) {
					// 	Message('登录过期','error');
					//   }
					resolve(data)
				} else {

					if (statusCode === 401) {

						if (uni.getStorageSync('cookie')) {
							Message('登录过期', 'error')
						}

						setTimeout(() => {
							toLogin()
						}, 1000)

					} else if (statusCode === 500) {
						if (data && data.message) {
							Message(message, 'error')
						}
					} else if (res) {
						Message(message)
					} else {
						Message('网络连接异常,请稍后再试!', 'error')
					}
				}
			},
			fail: (err) => {
				console.log(err)
				reject(err)
			}
		});
	})

}
export {
	config
}

export default requst
